package com.wxzz.elearing.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wxzz.elearing.user.dto.AdminNodeListDto;
import com.wxzz.elearing.user.dto.WorkNodeDto;
import com.wxzz.elearing.user.entity.*;
import com.wxzz.elearing.user.mapper.AdminUserMapper;
import com.wxzz.elearing.user.mapper.AdminWorkNodeMapper;
import com.wxzz.elearing.user.mapper.EnterpriseMapper;
import com.wxzz.elearing.user.mapper.WxUserMapper;
import com.wxzz.elearing.user.service.IAdminWorkNodeService;
import com.wxzz.elearing.user.vo.AdminWorkGuidelineVo;
import com.wxzz.elearing.user.vo.AdminWorkNodeVo;
import com.wxzz.elearning.common.exception.ServiceException;
import com.wxzz.elearning.common.vo.JgPageVo;
import com.wxzz.elearning.common.vo.ReturnVO;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 
 * @since 2021-05-16
 */
@Service
public class AdminWorkNodeServiceImpl extends ServiceImpl<AdminWorkNodeMapper, AdminWorkNode> implements IAdminWorkNodeService {

    @Resource
    private AdminWorkNodeMapper adminWorkNodeMapper;

    @Resource
    private WxUserMapper wxUserMapper;

    @Resource
    private EnterpriseMapper enterpriseMapper;
    public ReturnVO addAdminWorkNode(AdminWorkNode adminWorkNode){
        if(adminWorkNode.getId()==null){
            adminWorkNodeMapper.insert(adminWorkNode);
            return new ReturnVO("工作节点添加完成",null);
        }else{
            adminWorkNodeMapper.updateById(adminWorkNode);
            return new ReturnVO("工作节点修改成功",null);
        }
    }
    public ReturnVO<JgPageVo<List<AdminWorkNodeVo>>> getadminNodeList(AdminNodeListDto adminNodeListDto){
        Page<Object> objects = PageHelper.startPage(adminNodeListDto.getCurPage(), adminNodeListDto.getMaxPage());
        QueryWrapper queryWrapper = new QueryWrapper();
        if(StringUtils.isNotBlank(adminNodeListDto.getWorkTitle())){
            queryWrapper.like("work_title",adminNodeListDto.getWorkTitle()+"%");
        }
        if(adminNodeListDto.getWorkGuidelineId()==null||adminNodeListDto.getWorkGuidelineId()==0){
            throw new ServiceException("工作指引Id不能为空");
        }
        queryWrapper.eq("enterprise_id",adminNodeListDto.getEnterpriseId());
        queryWrapper.eq("work_guideline_id",adminNodeListDto.getWorkGuidelineId());
        queryWrapper.orderByDesc("create_time");
        List<AdminWorkNode> list = adminWorkNodeMapper.selectList(queryWrapper);
        List<AdminWorkNodeVo> adminWorkNodeVoArrayList = new ArrayList<>();
        list.forEach((adminWorkNode)->{
            AdminWorkNodeVo adminWorkGuidelineVo = new AdminWorkNodeVo();
            WxUser wxUser = wxUserMapper.selectById(adminWorkNode.getAdminUserId());
            Optional.ofNullable(wxUser).ifPresent(a-> adminWorkGuidelineVo.setAdminUserName(wxUser.getNickname()));
            Enterprise enterprise = enterpriseMapper.selectById(adminWorkNode.getEnterpriseId());
            Optional.ofNullable(enterprise).ifPresent(e->  adminWorkGuidelineVo.setEnterpriseName(enterprise.getEnterpriseName()));
            BeanUtils.copyProperties(adminWorkNode,adminWorkGuidelineVo);
            adminWorkNodeVoArrayList.add(adminWorkGuidelineVo);
        });
        long total = objects.getTotal();
        PageInfo pageInfo = new PageInfo(adminWorkNodeVoArrayList);
        pageInfo.setTotal(total);
        return new ReturnVO("工作指引详情查询成功",new JgPageVo<>(pageInfo));
//        return new ReturnVO("工作指引详情查询成功", new JgPageVo(new PageInfo<>(adminWorkNodeVoArrayList)));
    }

    public ReturnVO deleteWorkNode(WorkNodeDto workNodeDto){
        int i = adminWorkNodeMapper.deleteBatchIds(workNodeDto.getWorkNodeId());
        return new ReturnVO(i+"个节点删除成功",null);
    }
}
